package cn.yz.controller;

import cn.yz.dto.RoleDto;
import cn.yz.dto.RolePageDto;
import cn.yz.result.Result;
import cn.yz.service.RoleService;
import cn.yz.vo.PageVo;
import cn.yz.vo.RoleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@Tag(name = "角色管理", description = "角色管理相关接口")
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @PostMapping("/page")
    @Operation(summary = "分页查询角色列表", description = "分页查询角色列表")
    public Result<PageVo<RoleVo>> getRolePage(@Valid @RequestBody RolePageDto rolePageDto) {
        PageVo<RoleVo> pageVo = roleService.getRolePage(rolePageDto);
        return Result.success(pageVo);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户角色列表")
    public Result<List<RoleVo>> getUserRoles(@Parameter(description = "用户ID") @PathVariable("userId") String userId) {
        List<RoleVo> roles = roleService.getUserRoles(userId);
        return Result.success(roles);
    }

    @GetMapping("/list")
    @Operation(summary = "获取角色列表")
    public Result<List<RoleVo>> getRoleList(
            @Parameter(description = "角色名称") @RequestParam(name = "name", required = false) String name,
            @Parameter(description = "角色编码") @RequestParam(name = "code", required = false) String code,
            @Parameter(description = "状态") @RequestParam(name = "status", required = false) Integer status) {
        List<RoleVo> roles = roleService.getRoleList(name, code, status);
        return Result.success(roles);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取角色详情")
    public Result<RoleVo> getRoleById(@Parameter(description = "角色ID") @PathVariable("id") String id) {
        RoleVo role = roleService.getRoleById(id);
        return Result.success(role);
    }

    @PostMapping
    @Operation(summary = "创建角色")
    public Result<Void> createRole(@RequestBody RoleDto roleDto) {
        roleService.createRole(roleDto);
        return Result.success();
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新角色")
    public Result<Void> updateRole(@Parameter(description = "角色ID") @PathVariable("id") String id,
                                 @RequestBody RoleDto roleDto) {
        roleService.updateRole(id, roleDto);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    public Result<Void> deleteRole(@Parameter(description = "角色ID") @PathVariable("id") String id) {
        roleService.deleteRole(id);
        return Result.success();
    }

    @PostMapping("/user/{userId}/assign")
    @Operation(summary = "分配用户角色")
    public Result<Void> assignUserRoles(@Parameter(description = "用户ID") @PathVariable("userId") String userId,
                                      @RequestBody List<String> roleIds) {
        roleService.assignUserRoles(userId, roleIds);
        return Result.success();
    }
} 